<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/3/15
 * Time: 9:27
 */

namespace app\common\lib;


class TencentAsr
{
    public $secretid;

    public $secretkey;

    public $asrClient;

    protected static $instance;

    public function __construct()
    {

        $config = config('param.tencent_cloud');
        $this->secretid = $config['secretid'];
        $this->secretkey = $config['secretkey'];

        $credential = new \TencentCloud\Common\Credential($this->secretid ,$this->secretkey);
        $this->asrClient = new \TencentCloud\Asr\V20190614\AsrClient($credential,'');
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }


        return new self();
    }

    public function createAsyncRecognitionTask($videoUrl)
    {
        //var_dump($videoUrl);
        $request = (new \TencentCloud\Asr\V20190614\Models\CreateAsyncRecognitionTaskRequest());
        $request->setUrl($videoUrl);
        $request->setEngineType('16k_zh');
        $callbackUrl = rtrim(config('app.app_url'),'/').'/index/notify/asyncrecognitiontask';

        //var_dump($callbackUrl);
        $request->setCallbackUrl($callbackUrl);
        $request->setFilterDirty(0);
        $request->setFilterModal(0);
        $request->setFilterPunc(0);
        $request->setConvertNumMode(1);
        $request->setWordInfo(0);
        $request->setAudioData(true);
        $result = $this->asrClient->CreateAsyncRecognitionTask($request);

        return $result->serialize();
    }

    public function createRecTask($videoUrl)
    {
        $request = (new \TencentCloud\Asr\V20190614\Models\CreateRecTaskRequest());
        $request->setEngineModelType('16k_zh');
        $request->setChannelNum(1);
        $request->setResTextFormat(0);
        $request->setSourceType(0);
        $request->setUrl($videoUrl);
       // $callbackUrl = rtrim(config('app.app_url'),'/').'/index/notify/callbackrectask';
        //$request->setCallbackUrl($callbackUrl);
        $request->setSpeakerDiarization(0);
        $request->setSpeakerNumber(0);
        $request->setEmotionRecognition(0);
        $request->setEmotionalEnergy(0);
        $request->setConvertNumMode(0);
        $request->setFilterDirty(0);
        $request->setFilterPunc(0);
        $request->setFilterModal(0);
        $request->setSentenceMaxLength(0);
        $result = $this->asrClient->CreateRecTask($request);

        return $result->serialize();
    }

    public function describeTaskStatus($taskId)
    {
        $request = (new \TencentCloud\Asr\V20190614\Models\DescribeTaskStatusRequest());
        $request->setTaskId($taskId);
        $result = $this->asrClient->DescribeTaskStatus($request);

        return $result->serialize();
    }


}